package com.ofek.applicationbase.forms;

import java.io.Serializable;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.ofek.applicationbase.R;


public class SimpleQuestionView extends QuestionView {
	private TextView question;
	private EditText answer;
	private String resId;
	
	public SimpleQuestionView(final int questionStringId,final int inputType,final Context context) {
		super(R.layout.form_question_simple,context);
		init(context.getString(questionStringId));
		answer.setInputType(inputType);
	}
	public SimpleQuestionView(final int questionStringId,final int inputType,final Context context,final boolean required) {
		super(R.layout.form_question_simple,context,required);
		init(context.getString(questionStringId));
		answer.setInputType(inputType);
	}
	public SimpleQuestionView(final String questionStringId,final int inputType,final Context context,final boolean required) {
		super(R.layout.form_question_simple,context,required);
		init(questionStringId);
		answer.setInputType(inputType);
	}

	private void init(final String resId) {
		question = (TextView)findViewById(R.id.question);
		question.setText(resId+(isRequired() ? "*" : ""));
		this.resId=resId;
		answer = (EditText)findViewById(R.id.answer);
		answer.setHint(resId);
		answer.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				broadcastAnswerChanged(getAnswer());
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	@Override
	public Serializable getAnswer() {
		final String result = answer.getText().toString();
		final boolean valid = result.length()>0;
		return valid ? result : null;
	}
	@Override
	public void setRequired(final boolean required) { 
		super.setRequired(required);
		question.setText(resId+(isRequired() ? "*" : ""));
	}

	@Override
	public void setAnswer(final Object defaultAnswer) {
		if (defaultAnswer!=null) answer.setText(defaultAnswer.toString());
	}
	@Override
	public void animateInvalidInput() {
		question.setTextColor(Color.RED);
		question.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake));
	}

	@Override
	protected void broadcastAnswerChanged(Object newAnswer) {
		if (hasValidInput()) 
			question.setTextColor(Color.BLACK);
		super.broadcastAnswerChanged(newAnswer);
	}
	@Override
	public void setEditable(final boolean edit) {
//		Field key;
//		Object object;
//		try {
//			try {
//				Field field = TextView.class.getDeclaredField("mEditor");
//				field.setAccessible(true);
//				object = field.get(answer);
//				key = object.getClass().getDeclaredField("mKeyListener");
//			} catch (Exception e) {
//				key = TextView.class.getDeclaredField("mInput");
//				object = answer;
//			} 
//			key.setAccessible(true);
//			key.set(object, null);
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
	}
	@Override
	public void setDefaultAnswer(Object defaultAnswer, boolean shouldPrePick) {
		if (defaultAnswer!=null && shouldPrePick) 
			answer.setText(defaultAnswer.toString());
	}


}
